﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CapstoneProjectAEReader.DataAccess.DBAccess;
using CapstoneProjectAEReader.Common.Utility;
using CapstoneProjectAEReader.Entities.Models;

namespace CapstoneProjectAEReader.GUIs.Others
{
    /// <summary>
    /// Interaction logic for AddCategory.xaml
    /// </summary>
    public partial class AddItems : Window
    {
        #region Attributes

        private DAO _dao;
        // 0: new Group, 1: Update Group Info
        private int _mode;
        private EbookGroup _group = new EbookGroup();

        private const string AddNewGroup = "addNewGroup";
        private const string UpdateGroup = "updateGroup";
        private const string GroupName = "groupName";
        private const string Description = "description";
        private const string Save = "saveButton";
        private const string Cancel = "cancelButton";
        private const string GroupEbookExist = "ebookGroupExist";
        private const string GroupBlank = "groupNameBlank";

        #endregion

        public AddItems(DTO dto)
        {
            InitializeComponent();
            _mode = 0;
            _dao = new DAO(dto);
            ReloadFormLanguage();
        }

        public AddItems(DTO dto, EbookGroup oldGroup)
        {
            InitializeComponent();
            _group = oldGroup;
            _mode = 1;
            _dao = new DAO(dto);
            ReloadFormLanguage();
        }

        public void ReloadFormLanguage()
        {
            lbCancel.Content = ResourceReader.GetResourceContent(Cancel);
            lbSave.Content = ResourceReader.GetResourceContent(Save);
            lbName.Content = ResourceReader.GetResourceContent(GroupName);
            lbDescription.Content = ResourceReader.GetResourceContent(Description);
            if (_mode == 0)
            {
                lbContent.Content = ResourceReader.GetResourceContent(AddNewGroup);
            }

            if (_mode == 1)
            {
                lbContent.Content = ResourceReader.GetResourceContent(UpdateGroup);
                txtName.Text = _group.Groupname;
                FlowDocument tempDoc = new FlowDocument();
                tempDoc.Blocks.Add(new Paragraph(new Run(_group.Description)));
                txtDescription.Document = tempDoc;
            }
        }

        public void SetFormTitle(string title)
        {
            Title = title;
        }

        private void HandleBtSave(object sender, RoutedEventArgs e)
        {
            if (_mode == 0)
            {
                if (_dao.CheckEbookGroupExist(txtName.Text.Trim()))
                {
                    Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(GroupEbookExist));
                }
                else
                {
                    _group.Groupname = txtName.Text.Trim();
                    if (!String.IsNullOrWhiteSpace(_group.Groupname))
                    {
                        var textRange = new TextRange(txtDescription.Document.ContentStart,
                                                      txtDescription.Document.ContentEnd);
                        _group.Description = textRange.Text;
                        _dao.InsertEbookGroup(_group);
                        DialogResult = true;
                        Close();
                    }
                    else
                    {
                        Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(GroupBlank));
                    }
                }
            }

            if (_mode == 1)
            {
                if (_group.Groupname == txtName.Text.Trim())
                {
                    var textRange = new TextRange(txtDescription.Document.ContentStart,
                                                  txtDescription.Document.ContentEnd);
                    _group.Description = textRange.Text;
                    EbookGroup temp = new EbookGroup { Description = _group.Description, Groupid = _group.Groupid, Groupname = _group.Groupname };
                    _dao.UpdateEbookGroup(temp);
                    DialogResult = true;
                    Close();
                }
                else
                {
                    if (_dao.CheckEbookGroupExist(txtName.Text.Trim()))
                    {
                        Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(GroupEbookExist));
                    }
                    else
                    {
                        string tempGroup = txtName.Text.Trim();
                        if (!String.IsNullOrWhiteSpace(tempGroup))
                        {
                            var textRange = new TextRange(txtDescription.Document.ContentStart,
                                                          txtDescription.Document.ContentEnd);
                            _group.Description = textRange.Text;
                            EbookGroup temp = new EbookGroup { Description = _group.Description, Groupid = _group.Groupid, Groupname = tempGroup };
                            _dao.UpdateEbookGroup(temp);
                            DialogResult = true;
                            Close();
                        }
                        else
                        {
                            Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(GroupBlank));
                        }
                    }
                }
            }
        }

        private void HandleBtCancel(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}